package com.infotel.chat;

import java.io.IOException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;

import javax.swing.JOptionPane;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;
import org.w3c.dom.Text;
import org.xml.sax.SAXException;

public class TestChatXMLDom {

    /**
     * @param args
     */
    public static void main(String[] args) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS");

        try {


            DocumentBuilderFactory factory = DocumentBuilderFactory
                    .newInstance();
            DocumentBuilder builder = factory.newDocumentBuilder();
            Document document = builder.parse("src/com/infotel/chat/chat.xml");
            ArrayList<Users> users = new ArrayList<>();
            ArrayList<Messages> messages = new ArrayList<>();

            NodeList elementsUsers = document.getElementsByTagName("user");
            for (int i = 0; i < elementsUsers.getLength(); i++) {
                Element elementUser = (Element) elementsUsers.item(i);
                Users user = new Users();
                user.setId(elementUser.getAttribute("id"));
                user.setEmail(elementUser.getAttribute("email"));
                user.setGender(elementUser.getAttribute("gender"));
                users.add(user);
            }

            NodeList elementsMessages = document
                    .getElementsByTagName("message");
            for (int i = 0; i < elementsMessages.getLength(); i++) {
                Element elementMessage = (Element) elementsMessages.item(i);
                Messages message = new Messages();
                message.setAuteur(elementMessage.getAttribute("author"));
                try {
                    message.setDate(sdf.parse(elementMessage
                            .getAttribute("date")));
                } catch (ParseException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
                message.setContent(((Text) elementsMessages.item(i)
                        .getFirstChild()).getData());
                messages.add(message);
            }

            String utilisateur = JOptionPane.showInputDialog("utilisateur");
            for (Messages m : messages) {
                if (m.getAuteur().equals(utilisateur)) {
                    System.out.println(m.getContent() + "\n");
                }
            }
            

        } catch (ParserConfigurationException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (SAXException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

    }
}
